Ext.define('RDB.controller.WizardWindowController', {
    extend: 'Ext.app.Controller',
    init: function (){
	this.control({
	    'wizardwindow wizardpanel': {
		activate: this.onWizardPanelActivation,
	    },
	    'wizardwindow button[name=next]': {
		click: this.onClickNextButton,
	    },
	    'wizardwindow button[name=back]': {
		click: this.onClickBackButton,
	    },
	    'wizardwindow radio[name=wizardtype]': {
		change: this.onChangeRadioWizardType,
	    },
	    'wizardwindow': {
		show: this.onShowWizard,
	    },
	    'wizardwindow textfield[name=fileupload]': {
		change: this.onChangeFileUpload,
	    },
	    'wizardwindow button[name=cancel]': {
		click: this.onClickCancelButton,
	    },
	});
    },

    onClickCancelButton: function (button){
	var win = button.up('window');
	win.close();
    },

    onChangeFileUpload: function(field, newValue, oldValue, e){
	var form = field.up('form').getForm(),
	wizardLayout = field.up('panel[name=wizardlayout]').getLayout();
	form.submit({
	    url: '/bin/file_upload.py',
	    //waitMsg: 'Отправка файла...',
	    success: function (form, action) {
		console.log('success', arguments);
		wizardLayout.setActiveItem('wizardprocess');

		Ext.Ajax.request({
		    url: '/bin/web_api/xls_import.py/prepare_xls',
		    params: {
			prepare_file: action.result.file
		    },
		    success: function (response, op){
			console.log('success xls', arguments);
			var store = Ext.StoreManager.lookup('ImportStore');
			store.proxy.url = '/bin/web_api/xls_import.py/' +
			    'load_parse_json?file_name=' +
			    response.responseText;
			json_file = response.responseText;
			store.load();
			var al = wizardLayout.setActiveItem('wizardtables');
		    },
		    failure: function (response, op){
			wizardLayout.setActiveItem('wizarderror');
			var errtext = win.query('label[name=errormessage]')[0];
			errtext.setText('Error! Parsing Excel file!' +
					response);
		    }
		});
	    },
	    failure: function (form, action) {
		console.log('failure', arguments);
		wizardLayout.setActiveItem('wizarderror');
		var errtext = win.query('label[name=errormessage]')[0];
		errtext.setText('An error occurred while uploading the file to the server!');
	    },
	});
    },

    onShowWizard: function (win, e) {
	win.query('wizardpanel')[0].wizardConfig.nextLayout = "wizardimport";
    },

    onChangeRadioWizardType: function (radio, newValue, oldValue, e){
	var activeLayout = radio.up('wizardpanel');
	if (newValue) activeLayout.wizardConfig.nextLayout = radio.inputValue;
    },

    onWizardPanelActivation: function (panel, e) {
	var win = panel.up('wizardwindow'),
	backButton = win.query('button[name=back]')[0],
	nextButton = win.query('button[name=next]')[0],
	cancelButton = win.query('button[name=cancel]')[0];

	backButton.setDisabled(panel.wizardConfig.backButton.disabled);
	nextButton.setDisabled(panel.wizardConfig.nextButton.disabled);
	cancelButton.setDisabled(panel.wizardConfig.cancelButton.disabled);
    },

    onClickNextButton: function (button, e) {
	var win = button.up('wizardwindow'),
	wizardLayout = win.query('panel[name=wizardlayout]')[0].getLayout(),
	activeLayout = wizardLayout.activeItem;
	switch (activeLayout.itemId){
	case 'wizardtables':
	    wizardLayout.setActiveItem('wizardprocess');
	    Ext.Ajax.request({
		url: '/bin/web_api/xls_import.py/commit_import',
		params: {
		    file_name: json_file
		},
		success: function (response, op){
		    console.log('success commit', arguments);
		    win.close();
		},
		failure: function (response, op){
		    wizardLayout.setActiveItem('wizarderror');
		    var errtext = win.query('label[name=errormessage]')[0];
		    errtext.setText('Error! An error has occurred. ' +
				    'Import is not completed! ' +
				    response);
		}
	    });
	    break;
	}
	wizardLayout.setActiveItem(activeLayout.wizardConfig.nextLayout);
    },

    onClickBackButton: function (button, e) {
	var win = button.up('wizardwindow'),
	wizardLayout = win.query('panel[name=wizardlayout]')[0].getLayout(),
	activeLayout = wizardLayout.activeItem;
	wizardLayout.setActiveItem(activeLayout.wizardConfig.backLayout);
    },
});